package com.gui.common;

import cn.hutool.http.HttpStatus;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@Builder
public class Result<T> {

    private Integer code;

    private String message;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date timestamp;

    private T data;

    /**
     * response success result wrapper.
     *
     * @param <T> type of data class
     * @return response result
     */
    public static <T> Result<T> success() {
        return success(null);
    }

    /**
     * response success result wrapper.
     *
     * @param data response data
     * @param <T>  type of data class
     * @return response result
     */
    public static <T> Result<T> success(T data) {
        return Result.<T>builder()
                .data(data)
                .message("success")
                .code(HttpStatus.HTTP_OK)
                .timestamp(new Date())
                .build();
    }

    /**
     * response error result wrapper.
     *
     * @param message error message
     * @param <T>     type of data class
     * @return response result
     */
    public static <T extends Serializable> Result<T> fail(String message) {
        return fail(null, message);
    }

    /**
     * response error result wrapper.
     *
     * @param data    response data
     * @param message error message
     * @param <T>     type of data class
     * @return response result
     */
    public static <T> Result<T> fail(T data, String message) {
        return Result.<T>builder()
                .data(data)
                .message(message)
                .code(HttpStatus.HTTP_INTERNAL_ERROR)
                .timestamp(new Date())
                .build();
    }

}
